package sam.learn.biz.datasource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import sam.learn.biz.datasource.repo.entity.DatasourceInfo;
import sam.learn.biz.datasource.repo.mapper.DatasourceInfoMapper;
import sam.learn.biz.datasource.service.DatasourceInfoService;

import java.util.List;


@Service
public class DatasourceInfoServiceImpl implements DatasourceInfoService {

    @Autowired
    private DatasourceInfoMapper datasourceInfoMapper;

    @Override
    @CacheEvict(value = "user-center:datasource-info")
    public DatasourceInfo addDatasource(DatasourceInfo datasourceInfo) {
        datasourceInfoMapper.insert(datasourceInfo);
        return datasourceInfo;
    }


    @Override
    @Cacheable(value = "user-center:datasource-info", key = "#regionCode")
    public DatasourceInfo getDatasourceBy(String regionCode) {
        LambdaQueryWrapper<DatasourceInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>(DatasourceInfo.class);
        lambdaQueryWrapper.eq(DatasourceInfo::getRegionCode, regionCode);
        return datasourceInfoMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public List<DatasourceInfo> loadAllDatasourceInfo() {
        LambdaQueryWrapper<DatasourceInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>(DatasourceInfo.class);
        lambdaQueryWrapper.isNotNull(DatasourceInfo::getRegionCode);
        return datasourceInfoMapper.selectList(lambdaQueryWrapper);
    }


}
